package neu.soft.service;


import neu.soft.constant.OrderType;
import neu.soft.mapper.OrderMapper;
import neu.soft.rocket.OutputSource;
import neu.soft.vo.MsgBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SenderService {

    @Autowired
    OrderService orderService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OutputSource outputSource;
    //发送缺货单给业务中心
    public boolean sendMsgForLack(MsgBean msgBean){
        Message<MsgBean> message = MessageBuilder.withPayload(msgBean)
                .build();
        return outputSource.sendTrans().send(message);
    }
    //发送收到的钱
    public boolean sendMoney(MsgBean msgBean){
        Message<MsgBean> message = MessageBuilder.withPayload(msgBean)
                .build();
        return outputSource.sendMoney().send(message);
    }
//发送普通订单给调度中心
    public boolean sendMsg(MsgBean msgBean){
        Message<MsgBean> message = MessageBuilder.withPayload(msgBean)
                .build();
        return outputSource.sendCommon().send(message);
    }
    //发送退货单给调度中心
    public boolean sendMsgForReturn(MsgBean msgBean){
        Message<MsgBean> message = MessageBuilder.withPayload(msgBean)
                .build();
        return outputSource.sendReturn().send(message);
    }
    //完成的消息发送
    public boolean sendMsgForExchange(MsgBean msgBean){
        Message<MsgBean> message = MessageBuilder.withPayload(msgBean)
                .build();
        return outputSource.sendExchange().send(message);
    }

    @StreamListener("listen-warehouse")
    public void receiveListenWarehouse(@Payload MsgBean receiveMsg)
    {
        System.out.println("listen-warehouse receive: " + receiveMsg);
        Map<String,Object> map = new HashMap<>();
        //处理map
        map.put("type", OrderType.NORMAL_ORDER);

        map.put("oid",receiveMsg.getData());
        orderMapper.changOrderType(map);
    }


}
